package org.mark.storage.service.impl;

import org.mark.common.response.Result;
import org.mark.common.entity.FreshFood;
import org.mark.storage.entity.StorageInfo;
import org.mark.storage.mapper.FreshFoodMapper;
import org.mark.storage.mapper.StorageInfoMapper;
import org.mark.storage.service.FreshFoodService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class FreshFoodServiceImpl implements FreshFoodService {

    @Resource
    private FreshFoodMapper freshFoodMapper;

    @Resource
    private StorageInfoMapper storageInfoMapper;

    @Override
    public Result page(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<FreshFood> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(name)) {
            wrapper.like(FreshFood::getName, name);
        }
        wrapper.orderByDesc(FreshFood::getCreateTime);

        Page<FreshFood> page = freshFoodMapper.selectPage(
                new Page<>(pageNum, pageSize),
                wrapper
        );
        return Result.success(page);
    }

    @Override
    public Result save(FreshFood freshFood) {
        // 参数校验
        if (StringUtils.isEmpty(freshFood.getName())) {
            return Result.fail("名称不能为空");
        }
        if (freshFood.getStorageId() == null) {
            return Result.fail("请选择储位");
        }

        // 检查储位是否存在
        StorageInfo storage = storageInfoMapper.selectById(freshFood.getStorageId());
        if (storage == null) {
            return Result.fail("储位不存在");
        }
        freshFood.setStorageName(storage.getName());

        // 设置创建和更新信息
        freshFood.setCreateTime(new Date());
        freshFood.setUpdateTime(new Date());

        freshFoodMapper.insert(freshFood);
        return Result.success("添加成功");
    }

    @Override
    public Result update(FreshFood freshFood) {
        // 参数校验
        if (freshFood.getId() == null) {
            return Result.fail("ID不能为空");
        }
        if (StringUtils.isEmpty(freshFood.getName())) {
            return Result.fail("名称不能为空");
        }

        // 检查储位是否存在
        if (freshFood.getStorageId() != null) {
            StorageInfo storage = storageInfoMapper.selectById(freshFood.getStorageId());
            if (storage == null) {
                return Result.fail("储位不存在");
            }
            freshFood.setStorageName(storage.getName());
        }

        freshFood.setUpdateTime(new Date());
        freshFoodMapper.updateById(freshFood);
        return Result.success("更新成功");
    }

    @Override
    public Result remove(Integer id) {
        if (id == null) {
            return Result.fail("ID不能为空");
        }

        freshFoodMapper.deleteById(id);
        return Result.success("删除成功");
    }

    @Override
    public Result storageList() {
        List<StorageInfo> list = storageInfoMapper.selectList(null);
        return Result.success(list);
    }

    @Override
    @Transactional(readOnly = true)
    public Result list() {
        try {
            LambdaQueryWrapper<FreshFood> wrapper = new LambdaQueryWrapper<>();
            wrapper.orderByDesc(FreshFood::getCreateTime);
            List<FreshFood> list = freshFoodMapper.selectList(wrapper);
            return Result.success(list);
        } catch (Exception e) {
            log.error("获取生鲜列表失败", e);
            return Result.fail("获取生鲜列表失败");
        }
    }

    @Override
    public FreshFood getById(Integer id) {
        return freshFoodMapper.selectById(id);
    }
}